# -*- coding: utf-8 -*-
#
# Author:: hanjunfeng
# Date:: 2021/1/8


import threading
from time import sleep


class get_page_source(threading.Thread):
    def __init__(self, driver, interval=0.02, time_out=10, group=None, target=None, name=None,
                 args=(), kwargs=None, *, daemon=None):
        super().__init__(group=group, target=target, name=name, args=args, kwargs=kwargs, daemon=daemon)
        self.driver = driver
        self.interval = interval
        self.time_out = time_out

    # 存储超时时间内的page_source
    def run(self):
        global page_source_list_global
        page_source_list_global = []
        global try_times_global
        try_times = int(self.time_out / self.interval)
        for i in range(try_times):
            page_source_list_global.append(self.driver.page_source)
            sleep(self.interval)


# class monitor_for_change(threading.Thread):
#     def __init__(self, change_times, base_page_source, group=None, target=None, name=None,
#                  args=(), kwargs=None, *, daemon=None):
#         super().__init__(group=group, target=target, name=name, args=args, kwargs=kwargs, daemon=daemon)
#         self.change_times = change_times
#         self.base_page_source = base_page_source
#
#     def run(self):
#         global is_page_source_changed_global
#         is_page_source_changed_global = False
#         change_count = 0
#
#         _break = False
#         for i in range(try_times_global):
#             if _break:
#                 break
#             for i2 in range(500):
#                 try:
#                     if self.base_page_source != page_source_list_global[i]:
#                         self.base_page_source = page_source_list_global[i]
#                         change_count += 1
#                     if change_count >= self.change_times:
#                         is_page_source_changed_global = True
#                         _break = True
#                         break
#                 except:
#                     sleep(0.01)

def monitor_for_change():
    global is_page_source_changed_global
    is_page_source_changed_global = False
    change_count = 0

    _break = False
    for i in range(try_times_global):
        if _break:
            break
        for i2 in range(500):
            try:
                if self.base_page_source != page_source_list_global[i]:
                    self.base_page_source = page_source_list_global[i]
                    change_count += 1
                if change_count >= self.change_times:
                    is_page_source_changed_global = True
                    _break = True
                    break
            except:
                sleep(0.01)


'''
视觉方向：
判断当前页面（view区域）是否为空白页、有加载图

page_source方向：
传入动作前page_source
监测page_source更新状态
指定page_source更新次数
豁免为加载页的page_source

'''


# todo 获取页面加载时间
def wait_for_page_source_changed(driver_list, change_times=1, time_out=10):
    get_page_source(driver_list[1]).start()
    monitor_for_change(driver_list[1], change_times, self.page_source).start()
    for i in range(time_out):
        if is_page_source_changed_global == True:
            pass
        else:
            sleep(1)
